﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using OrdersDAL.EntityModel;
using log4net.Config;

namespace OrdersDAL
{
    public class BL
    {

        public static readonly ILog logger = LogManager.GetLogger(typeof(BL));

        public static Orders RetreiveNewOrder()
        {

            Orders result=null;
            try
            {
                using (var OrdersDataContext = new  TestOrders(Properties.Settings.Default.OrdersCompact))
                {
                    var neworder = (from orders in OrdersDataContext.Orders
                                   where orders.Status == "N"
                                   orderby orders.Id
                                   select orders).FirstOrDefault();

                    result = neworder;
                }
            }

            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }

            return result;
        }

        public bool ChangeOrderStatus(Orders order)
        {
            try
            {
                using (var OrdersDataContext = new TestOrders(Properties.Settings.Default.OrdersCompact))
                {
                    var ordertochangestatus = (from orders in OrdersDataContext.Orders
                                               where orders.Id == order.Id
                                               select orders).FirstOrDefault();
                   
                    if (ordertochangestatus != null)
                    {
                        ordertochangestatus.Status = "P";
                        ordertochangestatus.Date_Created = DateTime.Now;
                        OrdersDataContext.SubmitChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }

            return true;
        }

        public static bool CreateOrder(Orders order)
        {
            try
            {
                using (var OrdersDataContext = new TestOrders(Properties.Settings.Default.OrdersCompact))
                {
                    OrdersDataContext.Orders.InsertOnSubmit(order);
                    OrdersDataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return true;
        }

        public static bool ProcessOrder(Orders order)
        {
            try
            {
                using (var OrdersDataContext = new TestOrders(Properties.Settings.Default.OrdersCompact))
                {
                    ProcessedOrders processedorder = new ProcessedOrders()
                    {
                    Date_Created = DateTime.Now,
                    OrderId = order.Id,
                    Description = order.Description 
                    };
                
                    OrdersDataContext.ProcessedOrders.InsertOnSubmit(processedorder);
                    OrdersDataContext.SubmitChanges();
                
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return true;
        }

        
        public static bool CreateDemandCell(DemandCell demandCell)
        {
            try
            {
                using (var OrdersDataContext = new TestOrders(Properties.Settings.Default.OrdersCompact))
                {
                    OrdersDataContext.DemandCell.InsertOnSubmit(demandCell);
                    OrdersDataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return true;
        }



        public static List<DemandCell> GetDemandCells()
        {
            List<DemandCell> DemandCells = new List<DemandCell>();
            try
            {
                using (var OrdersDataContext = new TestOrders(Properties.Settings.Default.OrdersCompact))
                {
                    DemandCells = (from demandcells in OrdersDataContext.DemandCell
                                   select demandcells).ToList();
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return DemandCells;
        }
    }
}
